package com.pansoft.openplanet.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.efounder.chat.activity.BaseActivity;
import com.efounder.util.EnvSupportManager;
import com.pansoft.nfc.mifare.Common;
import com.pansoft.nfc.mifare.activity.MifareNfcMessageActivity;
import com.pansoft.openplanet.R;
import com.pansoft.openplanet.bean.CreateChildWalletBean;
import com.utilcode.util.ToastUtils;

/**
 * 选择设备类型
 * Created by maming on 19-3-26.
 */
public class ChooseEquipmentTypeActivity extends BaseActivity implements View.OnClickListener {
    //
    private TextView tv_title;
    //钱包bean
    private CreateChildWalletBean childWalletBean;

    public static void start(Context context, CreateChildWalletBean childWalletBean) {
        Intent starter = new Intent(context, ChooseEquipmentTypeActivity.class);
        starter.putExtra("childWalletBean", childWalletBean);
        context.startActivity(starter);
    }

    private CheckBox checkBox_intelligent_watch_choose, checkBox_hardware_wallet_choose;
    /**
     * 是否是智能手表的标记
     */
    private boolean isIntelligentWatch = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.open_planet_activity_choose_equipment_type);
        initView();
        initData();
        initListener();
    }

    /**
     * 初始化控件
     */
    private void initView() {
        tv_title = (TextView) findViewById(R.id.tv_title);
        checkBox_intelligent_watch_choose = (CheckBox) findViewById(R.id.checkBox_intelligent_watch_choose);
        checkBox_hardware_wallet_choose = (CheckBox) findViewById(R.id.checkBox_hardware_wallet_choose);
        //
        findViewById(R.id.cv_intelligent_watch).setOnClickListener(this);
        findViewById(R.id.cv_hardware_wallet).setOnClickListener(this);
        findViewById(R.id.linearLayout_buy_cold_wallet).setOnClickListener(this);
        findViewById(R.id.button_next_step).setOnClickListener(this);
    }

    /**
     * 初始化数据
     */
    private void initData() {
        //设置界面的标题
        tv_title.setText("选择设备类型");
        if (getIntent().getExtras() != null) {
            childWalletBean = (CreateChildWalletBean) getIntent().getExtras().get("childWalletBean");
        }
    }

    /**
     * 初始化监听事件
     */
    private void initListener() {
        //智能手表按钮的点击事件
        checkBox_intelligent_watch_choose.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                isIntelligentWatch = isChecked;
                checkBox_hardware_wallet_choose.setChecked(!isChecked);
            }
        });
        //NFC硬件钱包的点击事件
        checkBox_hardware_wallet_choose.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                isIntelligentWatch = !isChecked;
                checkBox_intelligent_watch_choose.setChecked(!isChecked);
            }
        });
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        //智能手表按钮的点击事件
        if (id == R.id.cv_intelligent_watch) {
            checkBox_intelligent_watch_choose.setChecked(true);
            //NFC硬件钱包的点击事件
        } else if (id == R.id.cv_hardware_wallet) {
            checkBox_hardware_wallet_choose.setChecked(true);
            //购买冷钱包按钮的点击事件
        } else if (id == R.id.linearLayout_buy_cold_wallet) {


            //下一步按钮的点击事件
        } else if (id == R.id.button_next_step) {
            addChildWalletToServer();
        }
    }

    private void addChildWalletToServer() {
        if (!checkBox_intelligent_watch_choose.isChecked() && !checkBox_hardware_wallet_choose.isChecked()) {
            ToastUtils.showShort(R.string.op_no_choose_equipment_type);
            return;
        }
        //如果选择智能手表不允许点击下一步按钮
        if (checkBox_intelligent_watch_choose.isChecked()) {
            return;
        }

        // 直接读取NFC
        if (!EnvSupportManager.isSupportNfcWallet() && !EnvSupportManager.isSupportForceBleNfc()) {
            return;
        }
        if (!EnvSupportManager.isSupportForceBleNfc() && Common.hasMifareClassicSupport(this)){
            MifareNfcMessageActivity.start(ChooseEquipmentTypeActivity.this, childWalletBean,
                    BleBlutoothScanActivity.TYPE_CREATE_COLD_WALLET);
            //蓝牙读卡器
        } else {
            BleBlutoothScanActivity.start(ChooseEquipmentTypeActivity.this, childWalletBean,
                    BleBlutoothScanActivity.TYPE_CREATE_COLD_WALLET, BleBlutoothScanActivity.BLE_TYPE_NFC);
        }
    }
}
